from flask import Flask
from flask import render_template, Response
from modules.test import Test
from controller.main import MainController, states


app = Flask(__name__)


@app.route("/")
def hello_world():
    return render_template('main.html')


@app.route("/test")
def test():
    return render_template('test.html')


@app.route('/getRes', methods=['POST'])
def getRes():
    '''
    返回
    {
        'fileCount': ,
        'content': []
    }
    '''
    return MainController.get_files_json()


@app.route('/upload', methods=['POST'])
def upload():
    return MainController.upload()


@app.route('/deleteFile', methods=['POST'])
def deleteFile():
    return MainController.deleteFile()

@app.route('/stream')
def stream():
    def generate():
        print("update event going on")
        while True:
            if states[0]:
                yield 'data: update\n\n'
                states[0] = False
    return Response(generate(), mimetype='text/event-stream')


@app.route('/download', methods=['POST'])
def download():
    return MainController.download()
